﻿@{
    ViewBag.Title = "CouponInfo";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/bill/bill.js")" type="text/javascript"></script> 
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />      
}

<form id="form1" method="post">
         @Html.Partial("~/Views/Modular/Toolbars.cshtml")

        <div id="tabs1" class="mini-tabs" activeIndex="0"  style="width:100%; height:100%; top: 0px; left: 0px;overflow:hidden;border:none;" plain="true"  
        buttons="#tabsButtons" onactivechanged="changeTabs">
            <div name="Base" title="礼券资料" style="height:100%;overflow:hidden;margin-left:20px; margin-top:0px;">
                <table style="height:100%;margin-left:20px; margin-top:0px;">
                    <tr>
                        <td width="80px" height ="28px" align="right">礼券类型：</td>
                        <td width="190px" height ="28px">
                            @Html.Partial("~/Views/Modular/LookupCouponClass.cshtml")
                        </td>
                        <td width="80px" height ="28px" align="right">使用期限：</td>
                        <td width="240px" height ="28px" colspan="2">
                            <input id="FDateType" name="FDateType" class="mini-combobox" style="width:240px;" TabIndex="6" value ="1" data ="DateType" onvaluechanged="TypeChange"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" height ="28px" align="right">礼券编码：</td>
                        <td width="200px" height ="28px">
                            <input name="FNumber" id="FNumber" class="mini-textbox" style="width:200px;" value="" TabIndex="1"/>
                        </td>
                        <td width="80px" height ="28px" align="right">
                            <div id="typeName">有效天数：</div></td>
                        <td width="240px" height ="28px" colspan="2">
                            <div id="type2" style="display:none;">
                                <table align="left" style="width:240px; left: -3px">
                                     <tr>
                                        <td width ="110px">
                                            <input class="mini-datepicker" name="FDate1" format="yyyy-MM-dd" style="width:110px;" value = ""  TabIndex="7"/></td>
                                        <td width ="16px">至</td>
                                        <td width ="110px">
                                            <input class="mini-datepicker" name="FDate2" format="yyyy-MM-dd" style="width:110px;" value = ""/></td>
                                     </tr>
                                </table>
                            </div>
                            <div id="type1">
                                <input id="FDays" name="FDays" class="mini-spinner" minValue="0" maxValue="99999999" changeOnMousewheel ="false" 
                                     style="width:240px;" TabIndex="7" showbutton="false"  format="#,0" value="1"/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" height ="28px" align="right">
                            礼券名称：</td>
                        <td width="200px" height ="28px">
                            <input id="FName" name="FName" class="mini-textbox " style="width:200px;" TabIndex="2"/>
                        </td>
                        <td width="80px" height ="28px" align="right">
                            最低消费：</td>
                        <td width="240px" height ="28px">
                            <input name="FMinValue" id="FMinValue" class="mini-spinner" minValue="0" maxValue="9999999"  showbutton="false" 
                                changeOnMousewheel ="false" style="width:240px;"  value="" TabIndex="8"/>
                        </td>
                   </tr>
                    <tr>
                        <td width="80px" height ="28px" align="right">
                            礼券面额：</td>
                        <td width="200px" height ="28px">
                            <input name="FValue" id="FValue" class="mini-spinner" minValue="0" maxValue="9999999"  showbutton="false" 
                                changeOnMousewheel ="false" style="width:200px;"  value="" TabIndex="3"/>
                        </td>
                        <td width="80px" height ="28px" align="right">
                            最高消费：</td>
                        <td width="240px" height ="28px">
                            <input name="FMaxValue" id="FMaxValue" class="mini-spinner" minValue="0" maxValue="9999999"  showbutton="false"
                                changeOnMousewheel ="false"  style="width:240px;"  value="" TabIndex="9"/>
                        </td>
                    </tr>
                     <tr>
                        <td width="80px" height="28px" align="right">卡券使用提醒：</td>
                        <td width="200px" height="28px" align="left">
                            <input type="text" id="FNotice" name="FNotice" class="mini-textbox" maxlength="30" vtype="rangeChar:1,32" width="200px"  TabIndex="4"/>
                        </td>
                        <td width="80px" height="28px" align="right">备注：</td>
                        <td width="240px" height="28px" align="left">
                            <input id="FRemark" name="FRemark" class="mini-textbox "  style="width:240px;" TabIndex="10"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" height="48px" align="right">使用方式：</td>
                        <td width="200px" height="48px" align="left">
                            <input id="FSelectType" name="FSelectType" class="mini-combobox" style="width:200px;" TabIndex="6" value ="0" data ="dSelectType"/>
                        </td>
                        <td width="80px" height="48px" align="right">使用说明：</td>
                        <td width="240px" height="48px" align="left">
                            <textarea type="text" id="FDescription" name="FDescription" class="mini-textarea" vtype="rangeChar:1,2048" width="240px" height="42px" TabIndex="11"></textarea>
                        </td>
                    </tr>
                    <tr id="typeOnline">
                        <td width="80px" height="28px" align="right">发行量：</td>
                        <td width="200px" height="28px" align="left">
                            <input id="FMaxCount" name="FMaxCount" class="mini-spinner "  minValue="0" maxValue="9999999"  showbutton="false"
                                changeOnMousewheel ="false"  style="width:200px;" value="" TabIndex="12"/>
                        </td>
                        <td width="80px" height ="28px" align="right">
                            <div id="typeName">领券时间：</div></td>
                        <td width="240px" height ="28px" colspan="2">
                            <div id="type4">
                                <table align="left" style="width:240px; left: -3px">
                                     <tr>
                                        <td width ="110px">
                                            <input class="mini-datepicker" name="FActiveStartDate" format="yyyy-MM-dd" style="width:110px;" value = ""/></td>
                                        <td width ="16px">至</td>
                                        <td width ="110px">
                                            <input class="mini-datepicker" name="FActiveEndDate" format="yyyy-MM-dd" style="width:110px;" value = ""/></td>
                                     </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" align="right">适用范围：</td>
                        <td align="left" >
                            <div id="FStoreFlag" name="FStoreFlag" class="mini-checkbox" value ="true" text="实体店"></div>
                            <div id="FOnlineFlag" name="FOnlineFlag" class="mini-checkbox" onvaluechanged="onlineChanged" value ="true" text="线上" style="margin-left:30px;"></div>
                        </td>
                         <td width="80px" align="right"></td>
                         <td  align="left">
                             <div id="FAutoSendFlag" name="FAutoSendFlag" class="mini-checkbox" readOnly="false" text="新会员自动发放礼券"></div>
                         </td>
                    </tr>
                </table>
            
            </div>
            <div id="time_goods" name="time_goods" title="商品列表" style="height:100%;overflow:hidden;">
                <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
                    <table>
                        <tr>
                            <td width="300px;">
                                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="ShowGoodsWin" plain="true">新增</a>
                                <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRows()" plain="true">删行</a>
                            </td>
                            <td width="200px;" align="right">
                                提示：以下任意商品可使用该礼券（未选择表示所有商品可用）;
                            </td>
                        </tr>
                    </table>       
                </div>
                <div class="mini-fit" >
                    <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" showPager="false"  pageSize="100"
                        borderStyle="border:1;" url="../Coupon/GetGoodsList"
                        allowCellSelect="true" allowCellEdit="true" showColumnsMenu="true" fitColumns="false"  
                        sortMode="client" multiSelect="true">
                        <div property="columns">
                            <div type="checkcolumn" ></div>
                            <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                            <div field="FNumber" name="FNumber" width="120" headeralign="center" allowsort="true">编码</div>
                            <div field="FName" name="FName" width="230" headeralign="center" allowsort="true">商品名称</div>
                            <div field="FSpec" name="FSpec" width="120" headeralign="center" allowsort="true">规格</div>
                            <div field="FUnitName" name="FUnitName" width="60" headeralign="center" allowsort="true">单位</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <input name="FItemID" class="mini-hidden" />
</form>
<div id="tabsButtons">    
    <div id="ck1" name="ck1"  class="mini-checkbox" style ="margin-right:20px" readOnly="false" value ="false" text="连续新增 "></div>
</div> 

<script>   
    var DateType = [{ "id": "0", "text": "不限制" },
                   { "id": "1", "text": "限制使用天数" },
                { "id": "2", "text": "固定使用日期" }];

    var dSelectType = [{ "id": "0", "text": "同一单可用多张券" },
               { "id": "1", "text": "同一单只能用一张券" }];

    mini.parse();

    var grid = mini.get("datagrid1");

    $(function () {
        var nowDate = new Date();
        mini.getbyName("FDate1").setValue(dateToStr(nowDate, 1));
        mini.getbyName("FDate2").setValue(dateToStr(nowDate, 2));
        mini.getbyName("FActiveStartDate").setValue(dateToStr(nowDate, 1));
        mini.getbyName("FActiveEndDate").setValue(dateToStr(nowDate, 2));

        $("#myfile").change(function () {
            var formData = new FormData();
            formData.append("myfile", document.getElementById('myfile').files[0]);
            formData.append("mSystemAppID", parent.window.getSystemAppID());
            $.ajax({
                url: "/Upload/WxUploadFile",
                type: "post",
                data: formData,
                contentType: false,
                processData: false,
                success: function (res) {
                    if (res.status == 1) {
                        $("#Image").attr("src", res.path);
                        mini.get("FLogoUrl").setValue(res.logoUrl);
                    }
                }
            });
        });
    });

    //加载资料
    function SetInfoData(data) {
        if (data.action == "new") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            mini.getbyName("FItemID").setValue('0');
            mini.getbyName("FCouponClassNumber").setValue(data.FClassID);
            mini.getbyName("FCouponClassNumber").setText(data.FClassNumber);
            mini.getbyName("FCouponClassName").setValue(data.FClassName);
            NewInfo();
        }
        else {
            $.post("GetCoupon", { json: "[{'FItemID':'" + data.FItemID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                mini.getbyName("FItemID").setValue(data.FItemID);
                form.setData(mdata.data[0]);
                if (mdata.data[0].FStoreFlag == 1)
                    mini.getbyName("FStoreFlag").setChecked(true);
                else
                    mini.getbyName("FStoreFlag").setChecked(false);
                if (mdata.data[0].FOnlineFlag == 1)
                    mini.getbyName("FOnlineFlag").setChecked(true);
                else
                    mini.getbyName("FOnlineFlag").setChecked(false);

                mini.getbyName("FDate1").setValue(mdata.data[0].FStartDate);
                mini.getbyName("FDate2").setValue(mdata.data[0].FEndDate);
                mini.getbyName("FActiveStartDate").setValue(mdata.data[0].FActiveStartDate);
                mini.getbyName("FActiveEndDate").setValue(mdata.data[0].FActiveEndDate);
                $("#Image").attr('src', mdata.data[0].FLogoUrl);
                TypeChange();
                $.post("GetCouponClass", { json: "[{'FItemID':'" + mdata.data[0].FClassID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
                    var mdata = mini.decode(data);
                    mini.getbyName("FCouponClassNumber").setValue(mdata.data[0].FItemID);
                    mini.getbyName("FCouponClassNumber").setText(mdata.data[0].FNumber);
                    mini.getbyName("FCouponClassName").setValue(mdata.data[0].FName);
                });
                LoadListData();
            });
        }
    }

    function LoadListData() {
        var sSystemAppID = parent.window.getSystemAppID();
        var classid = mini.getbyName("FItemID").getValue();
        if (classid != "0") {
            var json = "[{";
            json = json + "'FSystemAppID':" + "'" + sSystemAppID + "',";
            json = json + "'mClassID':" + "'" + classid + "',";
            json = json + "'mNumber':" + "'',";
            json = json + "'mDatatype':0";
            json = json + "}]";
            grid.load({ mjson: json });
        }
    }

    //新增资料
    function NewInfo() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("FName").setValue("");
        mini.getbyName("FValue").setValue("0");
        mini.getbyName("FMinValue").setValue("0");
        mini.getbyName("FMaxValue").setValue("0");
        mini.getbyName("FMaxCount").setValue("0");
        mini.getbyName("FRemark").setValue("");
        $.post('NewCoupon', { json: '', mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            mini.getbyName("FNumber").setValue(data);
        });
    }

    function validate() {
        if (mini.getbyName("FCouponClassNumber").getValue() == '') {
            mini.alert("礼券分类没有选择");
            return false;
        } 
        if (mini.getbyName("FNumber").getValue() == '') {
            mini.alert("礼券编码不能为空");
            return false;
        } 
        if (mini.getbyName("FName").getValue() == '') {
            mini.alert("礼券名称不能为空");
            return false;
        }
        return true;
    }

    //保存资料
    function SaveInfo() {
        if (!validate()) return;

        mini.getbyName("SaveInfo").setEnabled(false);
        var form = new mini.Form("#form1");
        var fdata = form.getData(true, false);
        fdata.goodsRows = grid.getData();
        //fdata.FLogoUrl = $("#Image")[0].src;
        var json = mini.encode(fdata);
        $.post("SaveInfo", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                if (fdata.ck1 == "true")
                    NewInfo();
                else
                    CloseWindow();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").setEnabled(true);
        });
    }

    function TypeChange() {
        setType(mini.getbyName("FDateType").getValue());
    }

    function setType(typeValue) {
        if (typeValue == 0) {
            $("#typeName").html("有效天数：");
            $("#type1").show();
            mini.getbyName("FDays").setEnabled(false);
            mini.getbyName("FDays").setValue(0);
            $("#type2").hide();
        }
        else if (typeValue == 1) {
            $("#typeName").html("有效天数：");
            $("#type1").show();
            mini.getbyName("FDays").setEnabled(true);
            $("#type2").hide();
        }
        else if (typeValue == 2) {
            $("#typeName").html("固定日期：");
            $("#type1").hide();
            $("#type2").show();
        }
    }

    //删除资料
    function removeRows() {
        var row = grid.getSelecteds();
        if (row) {
            mini.confirm("确定要删除选中的记录吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        SetRemoveRows();
                    }
                });
        } else {
            mini.alert("请选要删除的记录");
        }
    }

    function SetRemoveRows() {
        var FSystemAppID = parent.window.getSystemAppID();
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
        grid.deselectAll();
    }

    //选择商品表
    function ShowGoodsWin() {
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: '', mBillType: 15 };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action) {
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        var newRow = {
                            FItemID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                            FUnitName: rw.FUnitName
                        };
                        AddRow(newRow);
                    }
                }
            }
        });
    }

    function AddRow(row) {
        try {
            if (!ExistsInGrid(row.FItemID)) {
                grid.addRow(row, grid.getData().length);
            }
        } catch (e) {
            grid.addRow(row, 0);
        }
    }

    function ExistsInGrid(FItemID) {
        var ret = false;
        $.each(grid.getData(), function (x, e) {
            if (e.FItemID == FItemID) {
                ret = true;
                return false;
            }
        });
        return ret;
    }

    function onlineChanged() {
        if (mini.getbyName("FOnlineFlag").getValue() == "true") {
            $("#typeOnline").show();
        }
        else {
            $("#typeOnline").hide();
        }
    }
</script>